﻿#region License

// Another free, open solution by someone who gets it.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System.Xml.Linq;

namespace Patterns.Xml
{
	/// <summary>
	/// 	Flattens an xml element or attribute, keeping only the name and text value of the node.
	/// </summary>
	public class XTextNode
	{
		/// <summary>
		/// 	Gets or sets the name.
		/// </summary>
		/// <value>The name.</value>
		public XName Name{ get; set; }

		/// <summary>
		/// 	Gets or sets the value.
		/// </summary>
		/// <value>The value.</value>
		public string Value{ get; set; }

		/// <summary>
		/// 	Performs an implicit conversion from <see cref = "System.String" /> to <see cref = "XTextNode" />.
		/// </summary>
		/// <param name = "value">The value.</param>
		/// <returns>The result of the conversion.</returns>
		public static implicit operator XTextNode(string value)
		{
			return new XTextNode
			{
				Name = "text",
				Value = value
			};
		}

		/// <summary>
		/// 	Performs an implicit conversion from <see cref = "System.Xml.Linq.XElement" /> to <see cref = "XTextNode" />.
		/// </summary>
		/// <param name = "element">The element.</param>
		/// <returns>The result of the conversion.</returns>
		public static implicit operator XTextNode(XElement element)
		{
			return new XTextNode
			{
				Name = element.Name,
				Value = element.Value
			};
		}

		/// <summary>
		/// 	Performs an implicit conversion from <see cref = "System.Xml.Linq.XAttribute" /> to <see cref = "XTextNode" />.
		/// </summary>
		/// <param name = "attribute">The attribute.</param>
		/// <returns>The result of the conversion.</returns>
		public static implicit operator XTextNode(XAttribute attribute)
		{
			return new XTextNode
			{
				Name = attribute.Name,
				Value = attribute.Value
			};
		}

		/// <summary>
		/// 	Performs an implicit conversion from <see cref = "System.Xml.Linq.XNode" /> to <see cref = "XTextNode" />.
		/// </summary>
		/// <param name = "node">The node.</param>
		/// <returns>The result of the conversion.</returns>
		public static implicit operator XTextNode(XNode node)
		{
			if (node == null) return null;

			var element = node as XElement;
			return element ?? new XTextNode
			{
				Name = "text",
				Value = node.ToString()
			};
		}

		/// <summary>
		/// 	Performs an implicit conversion from <see cref = "System.Xml.Linq.XObject" /> to <see cref = "XTextNode" />.
		/// </summary>
		/// <param name = "obj">The obj.</param>
		/// <returns>The result of the conversion.</returns>
		public static implicit operator XTextNode(XObject obj)
		{
			if (obj == null) return null;

			var attribute = obj as XAttribute;
			if (attribute != null) return attribute;

			return obj as XNode;
		}
	}
}